// 混入 表格页数跳转保留选择状态
export default {
    data() {
        return {
            dataListSelections: [],
            cancleSelect: []
        }
    },
    methods: {
        // 清空已选数据
        clearSelectData() {
            this.dataListSelections = []
            this.selectCheckBox()
        },

        // 已选中
        selectCheckBox() {
            let tempArr = JSON.parse(JSON.stringify(this.dataListSelections));
            if (tempArr.length > 0) {
                for (let i = 0; i < this.tData.length; i++) {
                    for (let j = 0; j < tempArr.length; j++) {
                        if (this.tData[i].orderId == tempArr[j].orderId) {
                            this.$refs.multipleTable.toggleRowSelection(
                                this.tData[i],
                                true
                            );
                        }
                    }
                }
            }
            if (this.dataListSelections.length == 0) {
                for (let i = 0; i < this.tData.length; i++) {
                    this.$refs.multipleTable.toggleRowSelection(
                        this.tData[i],
                        false
                    );
                }
            }
        },

        // 单个选择框
        selectChange(value) {
            let result = [];
            for (let i = 0; i < this.tData.length; i++) {
                let obj = this.tData[i];
                let num = obj.orderId;
                let isExist = false;
                for (let j = 0; j < value.length; j++) {
                    let aj = value[j];
                    let n = aj.orderId;
                    if (n == num) {
                        isExist = true;
                        break;
                    }
                }
                if (!isExist) {
                    result.push(obj);
                }
            }
            this.cancleSelect = result
            let tempArr = this.dataListSelections
            this.dataListSelections = tempArr.concat(value)
            this.unique()
            return result
        },

        // 全选
        selectAllChange(value) {
            this.selectChange(value);
        },

        //全选
        handleSelectAll(value, type) {
            this.selectChange(value);
            this.dataListSelections = type === 'all' ? this.dataListSelections : [];
            this.selectCheckBox();
        },

        // 删掉取消选中的
        deleteCancle() {
            for (let i = 0; i < this.cancleSelect.length; i++) {
                for (let j = 0; j < this.dataListSelections.length; j++) {
                    if (this.cancleSelect[i].orderId == this.dataListSelections[j].orderId) {
                        this.dataListSelections.splice(j, 1)
                        j--
                    }
                }
            }
        },

        // 数组去重
        unique() {
            for (let i = 0; i < this.dataListSelections.length; i++) {
                for (let j = i + 1; j < this.dataListSelections.length; j++) {
                    if (this.dataListSelections[i].orderId == this.dataListSelections[j].orderId) { //第一个等同于第二个，splice方法删除第二个
                        this.dataListSelections.splice(j, 1);
                        j--;
                    }
                }
            }
            this.deleteCancle()
            return this.dataListSelections;
        },
    }
}
